package com.example.demo.service.impl;

import com.example.demo.service.FirmwareUpgradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class FirmwareUpgradeServiceImpl implements FirmwareUpgradeService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private PlatformTransactionManager transactionManager;

    @Override
    public void save(Map<String, Object> firmwareUpgrade) {
        if(firmwareUpgrade== null || firmwareUpgrade.isEmpty()){
            return;
        }
        Iterator<String> keys = firmwareUpgrade.keySet().iterator();
        StringBuffer sb = new StringBuffer("insert or replace into firmware_upgrade(");
        StringBuffer sbKey = new StringBuffer("");
        StringBuffer sbValue = new StringBuffer("");
        while (keys.hasNext()){
            String key = keys.next();
            Object value = firmwareUpgrade.get(key);
            if("id_".equals(key) && (value == null || "".equals(value)))
                continue;
            sbKey.append(key + ",");
            sbValue.append("'" + value + "',");
        }
        sb.append(sbKey.substring(0, sbKey.length() -1 )).append(")").append(" values(").
                append(sbValue.substring(0, sbValue.length() -1 )).append(")");

        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(status -> {
            int num = jdbcTemplate.update(sb.toString());
            return num;
        });
    }

    @Override
    public List<Map<String, Object>> findAll() {
        String sql = "select * from firmware_upgrade";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }
    @Override
    public List<Map<String, Object>> findBySql(String sql) {
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @Override
    public List<Map<String, Object>> findByMap(Map<String, Object> map) {
        StringBuffer sb = new StringBuffer("select * from firmware_upgrade where 1=1 and ");
        String orderBy = "";
        if(map!= null && !map.isEmpty()){
            String[] column = {"id_", "project_code", "version", "model", "force_version"};
            orderBy = map.get("orderBy") != null ? map.get("orderBy").toString() : " version desc ";
            for (int i = 0; i < column.length; i++) {
                if(map.get(column[i]) != null && !"".equals(map.get(column[i]))){
                    sb.append(column[i] + "='" + map.get(column[i]) + "' and ");
                }
            }
        }
        sb.delete(sb.length() - 5, sb.length());
        if (orderBy != null && !"".equals(orderBy)){
            sb.append(" order by " + orderBy);
        }
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
        return list;
    }

    @Override
    public Map findById(int id) {
        String sql = "select * from firmware_upgrade where id_="+id;
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (list.size() > 0){
            return list.get(0);
        }
        return new HashMap<>();
    }

    @Override
    public void deleteById(int id) {
        String sql = "delete from firmware_upgrade where id_="+id;
        TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
        transactionTemplate.execute(status -> {
            int num = jdbcTemplate.update(sql.toString());
            return num;
        });
    }

    @Override
    public Map findByProjectCode(String projectCode) {
        String sql = "select * from firmware_upgrade where project_code="+projectCode;
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (list.size() > 0){
            return list.get(0);
        }
        return new HashMap<>();
    }
    public Map findByProjectCodeMax(String projectCode) {
        String sql = "select * from firmware_upgrade where project_code='" + projectCode + "' order by version desc";
        List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);
        if (list.size() > 0)
            return list.get(0);
        return new HashMap<>();
    }
}
